package com.ruoyi.wechat.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.wechat.domain.AddressCode;
import com.ruoyi.wechat.mapper.AddressCodeMapper;
import com.ruoyi.wechat.service.AddressCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author zyj
 * @date 2022-08-22 16:31
 */
@Service
public class AddressCodeServiceImpl implements AddressCodeService {

    @Autowired
    private AddressCodeMapper addressCodeMapper;

    @Autowired
    private RedisCache redisCache;

    public static final String CITY_CODE ="city:";


    /**
     * 初始化地址编码
     */
    @PostConstruct
    private void initAddressCode(){
        List<AddressCode> addressCodes = addressCodeMapper.listAddressCode();
        for (AddressCode addressCode : addressCodes) {
            redisCache.setCacheObject(CITY_CODE+addressCode.getCity(),addressCode.getCode());
        }
    }

    /**
     * 获取地址编码
     */
    public String getAddressCode(String city){
        city = city.replace("市", "");
        city = city.replace("区", "");
        return redisCache.getCacheObject(CITY_CODE+city);
    }
}
